<?php

namespace App\Utils;

use Illuminate\Support\Str;

class Arr
{
    public static function List2Tree(array $list, string $pk = 'id', string $pPk = 'pid', string $child = 'children'): array
    {
        $tree = [];
        $refer = [];
        foreach ($list as &$item) {
            $refer[$item[$pk]] = &$item;
        }
        foreach ($list as &$data) {
            $pid = $data[$pPk];
            if (isset($refer[$pid])) {
                $p = &$refer[$pid];
                $p[$child][] = &$data;
            } else {
                $tree[] = &$data;
            }
        }
        return $tree;
    }

    public static function key0Studly(array $data): array
    {
        foreach ($data as $k => $v) {
            if (str_contains($k, '_')) {
                $temp[lcfirst(Str::studly($k))] = $v;
            } else {
                $temp[$k] = $v;
            }
        }
        return $temp ?? [];
    }

    public static function key0Snake(array $data): array
    {
        foreach ($data as $k => $v) {
            $temp[Str::snake($k)] = $v;
        }
        return $temp ?? [];
    }

}
